$(function () {

  // 点击标题切换内容
  $(document).on('click', '.faq-title-description', function () {
    const $content = $(this).siblings(".faq-content"); // 找到当前 li 下的内容
    const $icon = $(this).find("svg"); // 找到箭头图标
    // 切换显示内容
    $content.stop(true, true).slideToggle(200);
    // 切换箭头方向（通过添加类名控制）
    $icon.toggleClass("open");
  })

  function initScrollCarousel (options) {
    const {
      wrapperSelector,
      itemSelector,
      prevButtonSelector,
      nextButtonSelector,
      gap = 20,
      throttleWait = 500
    } = options;

    const wrapper = document.querySelector(wrapperSelector);
    const productItem = document.querySelector(itemSelector);
    if (!wrapper || !productItem) return;

    const scrollStep = parseInt(productItem.getBoundingClientRect().width) + gap;

    // 节流函数
    function throttle (func, wait = 500) {
      let previous = 0;
      return function () {
        const now = +new Date();
        if (now - previous > wait) {
          func.apply(this, arguments);
          previous = now;
        }
      };
    }

    // 滑动逻辑
    const scrollLeftFn = throttle(() => {
      const currentScrollLeft = wrapper.scrollLeft;
      if (currentScrollLeft > 0) {
        $(wrapper).stop(true, true).animate(
          { scrollLeft: currentScrollLeft - scrollStep },
          'slow'
        );
      }
    }, throttleWait);

    const scrollRightFn = throttle(() => {
      const currentScrollLeft = wrapper.scrollLeft;
      const maxScrollLeft = wrapper.scrollWidth - wrapper.clientWidth;
      if (currentScrollLeft < maxScrollLeft) {
        $(wrapper).stop(true, true).animate(
          { scrollLeft: currentScrollLeft + scrollStep },
          'slow'
        );
      }
    }, throttleWait);

    // 使用事件委托绑定点击事件（动态按钮也能生效）
    $(wrapperSelector).on('click', prevButtonSelector, scrollLeftFn);
    $(wrapperSelector).on('click', nextButtonSelector, scrollRightFn);

    // 更新按钮状态函数
    function updateButtonState () {
      const maxScrollLeft = wrapper.scrollWidth - wrapper.clientWidth;
      const currentScrollLeft = wrapper.scrollLeft;

      const buttonLeft = wrapper.querySelector(prevButtonSelector);

      const buttonRight = wrapper.querySelector(nextButtonSelector);

      if (buttonLeft) {
        if (currentScrollLeft <= 0) {
          buttonLeft.classList.remove('slide-button-active');
        } else {
          buttonLeft.classList.add('slide-button-active');
        }
      }

      if (buttonRight) {
        if (currentScrollLeft >= maxScrollLeft - 1) {
          buttonRight.classList.remove('slide-button-active');
        } else {
          buttonRight.classList.add('slide-button-active');
        }
      }
    }

    // 监听滚动事件
    wrapper.addEventListener('scroll', updateButtonState);

    // 等按钮渲染完成后手动触发一次状态更新
    setTimeout(updateButtonState, 50);
  }


  //获取 Latest Products 板块（自动抓取数据）
  function getLatestProducts () {
    const $container = $(".guide-popular-designs-content .cards-wrapper");
    $container.on("click", ".addBag", function () {
      const rowData = JSON.parse($(this).parents(".product-card-item").attr("data-item"))
      LocalStore.update(
        'storeBagList',
        function (oldValue) {
          oldValue = Array.isArray(oldValue) ? oldValue : [] // 确保旧值是数组
          oldValue.push(rowData) // 追加新数据
          return oldValue
        },
        'id'
      )
      DMshowToast('Success!')
    });

    $container.on("click", ".inquiry", function () {
      const rowData = $(this).parents(".product-card-item").attr("data-item")
      window.location.href = `/view/产品详情页/index.html?id=${JSON.parse(rowData).id}`
    })

    initScrollCarousel({
      wrapperSelector: '.guide-popular-designs-content .cards-wrapper',
      itemSelector: '.guide-popular-designs-content .product-card-item',
      prevButtonSelector: '.button-prev',
      nextButtonSelector: '.button-next',
      gap: 20, // 卡片之间的间距
      throttleWait: 500 // 节流时间
    });
  }


  getLatestProducts()
})